// pages/options/options.js
var app = getApp();
let util = require("../../utils/util.js");
let baseImgUrl = util.baseImgUrl;

var x, y, x1, y1, x2, y2, index, currindex, n, yy;
var arr1 = [
];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    networkStatus: true,
    loadingStatus: false,//默认加载完成，true--正在加载中
    loadgif: util.imgUrl + "loading.gif",//加载gif图
    loading: util.imgUrl + "loading.png",
    none_network: util.imgUrl + "none_network.png",

    mainx: 0,
    closePage: baseImgUrl + "closePage.png",
    shutpic: baseImgUrl + "shutpic.png",
    add: baseImgUrl + "add.png",
    start: { x: 0, y: 0 },
    editing: false,
    navId2: "",//识堂选择的导航
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.navId);
    if (options != undefined) {
      wx.setStorageSync('reason', options.reason);
    }
    var that = this;
    var navId2 = app.globalData.navId2;
    var nav_index2 = app.globalData.nav_index2;
    that.setData({
      navId: navId2
    })
    console.log(options);

    //获取屏幕宽高
    that.setData({
      loadingStatus: true
    })
    /*********判断网络状态******************/
    wx.getNetworkType({
      success: function (res) {
        var networkType = res.networkType;
        if (networkType == "none" || networkType == "unknown") {
          that.setData({
            networkStatus: false
          })
        } else {
          that.setData({
            networkStatus: true,

          })
        }
      }, fail: function (err) {
        console.log("获取网络失败" + err);
        that.setData({
          networkStatus: false
        })
      }
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.windowHeight,
          screenWidth: res.windowWidth,
        });
      }
    });

    //请求后台获取数据
    //获取我的频道
    wx.request({
      url: util.pindao_my,
      data: {
        openid: app.globalData.openid,
        reason: wx.getStorageSync('reason'),
      },
      success: function (res) {
        that.setData({
          loadingStatus: false
        })
        console.log("---------wodepindao---------");
        console.log(res);
        that.setData({
          myoptionlist: res.data.my,
          otheroptionlist: res.data.gong,
        })
      },
      fail: function (err) {
        console.log("err" + err);
        that.setData({
          loadingStatus: true
        })
      }
    })


  },
  /********点击标签，跳转页面******* */
  tiaozhuan: function (e) {
    var that = this;
    var thisId = e.currentTarget.dataset.id;
    var flag = that.data.editing;
    if (flag) {
      //编辑状态下，目前不做操作
    } else {
      app.globalData.navId2 = thisId;
      app.globalData.nav_index2=parseInt(e.currentTarget.dataset.index)+3;
      console.log(app.globalData.navId);
      wx.navigateBack({
        delta: 1
      })
    }


  },
  //点击编辑开始编辑
  showEdit: function () {
    var that = this;
    that.setData({
      editing: true,
      navId: "t"
    })
  },


  //点击完成
  complete: function () {
    var that = this;
    that.setData({
      editing: false,
      navId: app.globalData.navId
    })
  },

  //关闭当前页
  closePage: function () {
    console.log("close");

    wx.navigateBack({
      delta: 2
    })


  },

  //删除已选频道
  removeOption: function (e) {
    var that = this;
    var classid = e.currentTarget.dataset.id;
    console.log(classid);
    wx.showModal({
      title: '',
      content: '您确认删除此频道',
      confirmColor: "#ff1313",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          //调用接口，将当前选项id传后台，重新刷新页面
          wx.request({
            url: util.del_pindao,
            data: {
              openid: app.globalData.openid,
              classid: classid
            },
            success: function (res) {
              if (classid == app.globalData.navId) {
                app.globalData.navId = "t";
                app.globalData.nav_index = 0;
              }

              console.log("删除成功")
              console.log(res);
              that.myOption()
            },
            fail: function (err) {
              console.log("删除失败" + err)
            }
          })
        //   //刷新页面，重新请求我的频道和其他频道
         
        //   wx.request({
        //     url: util.pindao_my,
        //     data: {
        //       openid: app.globalData.openid,
        //       reason: wx.getStorageSync('reason'),
        //     },
        //     success: function (res) {
        //       that.setData({
        //         loadingStatus: false
        //       })
        //       console.log("---------wodepindao---------");
        //       console.log(res);
        //       that.setData({
        //         myoptionlist: res.data.my,
        //         otheroptionlist: res.data.gong,
        //       })
        //     },
        //     fail: function (err) {
        //       console.log("err" + err);
        //       that.setData({
        //         loadingStatus: true
        //       })
        //     }
        //   })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }


      }
    })



  },

  //添加频道
  addOption: function (e) {
    var that = this;
    var classid = e.currentTarget.dataset.id;
    console.log("用户添加频道，请求后台，刷新页面");
    wx.showModal({
      title: '',
      content: '您确认添加此频道',
      confirmColor: "#ff1313",
      success(res) {
        if (res.confirm) {
          //添加接口
          wx.request({
            url: util.add_pindao,
            data: {
              classid: classid,
              openid: app.globalData.openid
            },
            success: function (res) {
              console.log(res)
              //刷新页面，重新请求我的频道和其他频道
              that.myOption();
            },
            fail: function (err) {
              console.log("err" + err)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })





  },

  myOption: function () {
    var that = this;
    //获取我的频道
    wx.request({
      url: util.pindao_my,
      data: {
        openid: app.globalData.openid,
        reason: wx.getStorageSync('reason'),
      },
      success: function (res) {
        console.log(res);
        that.setData({
          loadingStatus: false
        })

        that.setData({
          myoptionlist: res.data.my,
          otheroptionlist: res.data.gong,
        })
      },
      fail: function (err) {
        console.log("err" + err);
        that.setData({
          loadingStatus: true
        })
      }
    })
  },

  //拖拽改变频道的顺序
  movestart: function (e) {
    currindex = e.target.dataset.index;
    x = e.touches[0].clientX;
    y = e.touches[0].clientY;
    x1 = e.currentTarget.offsetLeft;
    y1 = e.currentTarget.offsetTop;
  },
  move: function (e) {
    yy = e.currentTarget.offsetTop;
    x2 = e.touches[0].clientX - x + x1;
    y2 = e.touches[0].clientY - y + y1;
    this.setData({
      mainx: currindex,
      opacity: 0.7,
      start: { x: x2, y: y2 }
    })
  },
  moveend: function () {
    var that = this;
    if (y2 != 0) {
      var arr = [];
      for (var i = 0; i < that.data.myoptionlist.length; i++) {
        arr.push(that.data.myoptionlist[i]);
        console.log("******arr[i]*******")
        console.log(arr[i])
      }

      var nx = that.data.myoptionlist.length;
      n = 1;
      for (var k = 2; k < nx; k++) {
        if (y2 > (52 * (k - 1) + k * 2 - 26)) {
          n = k;
        }
      }
      if (y2 > (52 * (nx - 1) + nx * 2 - 26)) {
        n = nx;
      }
      console.log("------------")
      arr = arr1;
      console.log(arr);
      console.log(arr1)

      arr.splice((currindex - 1), 1);
      arr.splice((n - 1), 0, arr1[currindex - 1]);
      arr1 = [];
      for (var m = 1; m < that.data.myoptionlist.length; m++) {
        console.log("arr[m]" + arr[m]);
        arr[m].id = m + 1;
        arr1.push(arr[m]);
      }

      this.setData({
        mainx: "",
        myoptionlist: arr,
        opacity: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})